package com.ctrip.framework.apollo.openapi.client.service;

import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.openapi.client.url.OpenApiPathBuilder;
import com.ctrip.framework.apollo.openapi.dto.OpenClusterDTO;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

public class ClusterOpenApiService extends AbstractOpenApiService implements com.ctrip.framework.apollo.openapi.api.ClusterOpenApiService {

    public ClusterOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson) {
        super(client, baseUrl, gson);
    }

    @Override
    public OpenClusterDTO getCluster(String appId, String env, String clusterName) {
        checkNotEmpty(appId, "App id");
        checkNotEmpty(env, "Env");

        if (Strings.isNullOrEmpty(clusterName)) {
            clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
        }

        OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
                .envsPathVal(env)
                .appsPathVal(appId)
                .clustersPathVal(clusterName);

        try (CloseableHttpResponse response = get(pathBuilder)) {
            return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenClusterDTO.class);
        } catch (Throwable ex) {
            throw new RuntimeException(String.format("Get cluster for appId: %s, cluster: %s in env: %s failed", appId, clusterName, env), ex);
        }
    }

    @Override
    public OpenClusterDTO createCluster(String env, OpenClusterDTO openClusterDTO) {
        checkNotEmpty(openClusterDTO.getAppId(), "App id");
        checkNotEmpty(env, "Env");
        checkNotEmpty(openClusterDTO.getName(), "Cluster name");
        checkNotEmpty(openClusterDTO.getDataChangeCreatedBy(), "Created by");

        OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
                .envsPathVal(env)
                .appsPathVal(openClusterDTO.getAppId())
                .customResource("clusters");

        try (CloseableHttpResponse response = post(pathBuilder, openClusterDTO)) {
            return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenClusterDTO.class);
        } catch (Throwable ex) {
            throw new RuntimeException(String
                    .format("Create cluster: %s for appId: %s in env: %s failed", openClusterDTO.getName(),
                            openClusterDTO.getAppId(), env), ex);
        }
    }
}
